<template>
	<view>
		<navBar :title="titles"></navBar>
		<view class="content">
			<view class="content-img">
				<img class="content-bigimg" :src="blogdescList.cover" alt="">
				<view class="content-text-box">
					<view class="content-text-top">
						{{blogdescList.title}}
					</view>

					<view class="content-text-bottom">
						<view class="content-text-bottom-left">
							<img class="userimg" :src="blogavatar" alt="">
							{{blogname}}
						</view>

						<view class="content-text-bottom-right">
							{{blogdescList.read_count}}阅读
						</view>
					</view>
				</view>
			</view>

			<view class="content-mian" v-html="blogdescList.content">
				{{blogdescList.content}}
			</view>

			<view class="comment_box">
				<h1>评论区</h1>

				<view class="comment_item" v-for="(item,index) in releaseList" :key="item.id">
					<view class="comment_box_user_left">
						<image :src="item.userAvatar" mode=""></image>
					</view>

					<view class="comment_box_user_right">
						<view class="comment_username">
							{{item.userName}}
						</view>
						<view class="usertext">
							{{item.content}}
						</view>

						<view class="qtuser_box">
							<text>{{formateDate(item.createdAt)}}</text>
							<view class="qtuser_box_img">
								<image :src="item.likeCount > 0  ? '../../static/commentdz2.png' : '../../static/commentdz1.png'" mode="" @click="likeComment(item)"></image>
								<view class="qtuser_box_hui">
									<text>回复</text>
								</view>
							</view>
						</view>

						<view class="line"></view>
					</view>
				</view>


			</view>
			
		</view>

		<view class="bottom_comment">
			<input @click="popup" class="bottom_ipt" type="text" placeholder="说点什么...">
			<view class="bottom_right">
				<view class="bottom_right_box">
					<image :src="likeUserId == userId ? '../../static/dianzan.png' : '../../static/undianzan.png'"
						mode="" @click="likeBlog"></image>
					<text>赞</text>
				</view>

				<view class="bottom_right_box">
					<image
						:src="followUserIdz.some((ele)=>{return ele.followUserId == userId})?'../../static/收藏 (1).png':'../../static/收藏.png'"
						mode="" @click="followavatar"></image>
					<text>关注作者</text>
				</view>

				<view class="bottom_right_box">
					<image src="../../static/评论.png" mode="" @click="popup"></image>
					<text @click="popup">评论</text>
				</view>
			</view>

			<u-popup :show="show" mode="bottom" @close="close" @open="open">
				<view>
					<u--textarea v-model="commentValue" placeholder="请输入内容" confirmType="done" count height="180" maxlength="1000">

					</u--textarea>
					<u-button @click="release" type="primary" size="small" text="发布"></u-button>
				</view>
			</u-popup>
		</view>
	</view>
</template>

<script>
	import {
		getBlogById,
		likeBlog
	} from "@/config/api.js"

	import {
		mapState
	} from 'vuex'

	import {
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				// 博客数据列表
				blogdescList: [],
				// 博客名称
				titles: 'kong',
				// 博客作者图像
				blogavatar: '',
				// 博客作者名称
				blogname: '',
				// 博客id
				id: "",
				// 点赞用户id
				likeUserId: "",
				// 用户id
				userId: "",
				// 关注作者列表
				followUserIdz: [],
				// 评论框隐藏/显示
				show: false,
				// 评论内容
				commentValue: '',
				// 评论列表
				releaseList: []
			}
		},
		onLoad(options) {
			let _this = this;
			this.id = options.id

			// this.setUser(res.data.data)
			uni.request({
				url: 'https://eggblog.bluej.cn/api/user/searchUser', //查看用户信息。
				data: {
					id: this.userInfo.id,
					projectId: 168
				},
				header: {
					"x-token": this.userInfo.token //自定义请求头信息
				},
				success: (res) => {
					console.log(res);
					_this.userId = res.data.data.id
					console.log(_this.userId);
					_this.blogavatar = res.data.data.avatar
					_this.blogname = res.data.data.name
					console.log(_this.blogavatar, _this.blogname);
				}
			})
		},
		onShow() {
			this.s();
			this.getRelease();
		},
		methods: {
			...mapMutations(['setUser']),
			likeBlog() {
				uni.request({
					method: "POST",
					url: 'https://eggblog.bluej.cn/api/blog/likeBlog', //点赞博客
					data: {
						id: this.id,
						projectId: 168
					},
					header: {
						"x-token": this.userInfo.token //自定义请求头信息
					},
					success: (res) => {
						console.log(res);
						if (res.data.msg == "取消点赞成功") {
							this.s();
						} else {
							this.s();
						}

					}
				})

			},
			s() {
				getBlogById(this.id).then(res => {
					console.log(res);

					console.log(this.likeUserId);
					this.blogdescList = res.data
					console.log(this.blogdescList);
					this.titles = res.data.title
					this.likeUserId = ""

					// if (res.data.follow.rows[0].length == 0) return
					this.followUserIdz = this.blogdescList.follow.rows
					console.log(this.followUserIdz);
					if (res.data.LikeBlogs.length == 0) return
					this.likeUserId = res.data.LikeBlogs[0].likeUserId;

				})
			},
			followavatar() {
				uni.request({
					method: "POST",
					url: 'https://eggblog.bluej.cn/api/user/follow', // 关注作者
					data: {
						id: this.userId,
						projectId: 168
					},
					header: {
						"x-token": this.userInfo.token //自定义请求头信息
					},
					success: (res) => {
						console.log(res);

						if (res.data.msg == "关注成功") {
							this.s();
						} else {
							this.s();
						}
					}
				})
			},

			open() {
				// console.log('open');
			},
			close() {
				this.show = false
				// console.log('close');
			},
			popup() {
				this.show = true
			},
			release() {
				uni.request({
					method: "POST",
					url: 'https://eggblog.bluej.cn/api/comment/create', // 发布评论
					data: {
						blogId: this.id,
						content: this.commentValue,
						projectId: 168
					},
					header: {
						"x-token": this.userInfo.token //自定义请求头信息
					},
					success: (res) => {
						console.log(res);
						this.commentValue = ''
						this.show = false;
						this.getRelease();
					}
				})
			},
			getRelease() {
				uni.request({
					url: 'https://eggblog.bluej.cn/api/comment/get', // 获取评论
					data: {
						id: this.id,
						projectId: 168,
						limit: 10,
						offset: 0,
						

					},
					header: {
						"x-token": this.userInfo.token //自定义请求头信息
					},
					success: (res) => {
						console.log(res);
						this.releaseList = res.data.data.rows
					}
				})
			},
			addZero(num) {
				return num < 10 ? '0' + num : num;
			},
			formateDate(date) {
				const arr = date.split('T');
				const d = arr[0];
				const darr = d.split('-');
			
				const t = arr[1];
				const tarr = t.split('.000');
				const marr = tarr[0].split(':');
			
				const dd =
					parseInt(darr[0]) +
					'年' +
					parseInt(darr[1]) +
					'月' +
					parseInt(darr[2]) +
					'日' + ' ' +
					this.addZero(parseInt(marr[0]) + 8) +
					'时' +
					this.addZero(parseInt(marr[1])) +
					'分'
				return dd;
			},
			likeComment(item){
				uni.request({
					method:'POST',
					url: 'https://eggblog.bluej.cn/api/comment/like', // 点赞评论
					data: {
						commentId:item.id,
						blogId: this.id,
						projectId: 168
						
					},
					header: {
						"x-token": this.userInfo.token //自定义请求头信息
					},
					success: (res) => {
						console.log(res);
						this.getRelease();
					}
				})
			}
		},
		computed: {
			...mapState(['userInfo'])
		},
	}
</script>

<style lang="scss">
	.content {
		width: 100vw;
		padding: 15px;
		box-sizing: border-box;

		.content-img {
			position: relative;
			width: 92vw;
			height: 304px;

			.content-bigimg {
				width: 92vw;
				height: 100%;
				filter: blur(3px);
			}

			.content-text-box {
				width: 92vw;
				box-sizing: border-box;
				padding: 6px;
				display: flex;
				flex-direction: column;
				height: 74px;
				justify-content: space-between;
				position: absolute;
				top: 233px;
				background-color: rgba(0, 0, 0, .3);

				.content-text-top {
					font-weight: bold;
					color: #fff;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}

				.content-text-bottom {
					display: flex;
					align-items: flex-end;
					width: 82vw;
					justify-content: space-between;

					.content-text-bottom-left {
						width: 40vw;
						font-size: 12px;
						color: #fff;
						display: flex;
						align-items: flex-end;


						.userimg {
							width: 30px;
							height: 30px;
							border-radius: 4px;
							margin-right: 10px;
						}
					}

					.content-text-bottom-right {
						display: flex;
						color: #fff;
						align-items: flex-end;
					}
				}
			}
		}

		.content-mian {
			margin-top: 40rpx;
			width: 92vw;
			color: $uni-text-color;
			font-size: $uni-font-size-base;
			line-height: 24px;
		}

		.comment_box {
			margin-bottom: 42px;
			margin-top: 15px;

			h1 {
				font-size: 24px;
				margin-bottom: 10px;
			}

			.comment_item {
				display: flex;
				justify-content: space-between;
				margin-bottom: 15px;

				.comment_box_user_left {
					width: 48px;
					height: 48px;

					image {
						width: 48px;
						height: 48px;
						border-radius: 50%;
					}
				}

				.comment_box_user_right {
					box-sizing: border-box;

					.comment_username {
						color: #f40004;
						font-size: 13px;
						margin-bottom: 10px;
					}

					.usertext {
						font-size: 15px;
						color: #000;
						width: 278px;
						word-wrap: break-word;
						line-height: 24px;
						margin-bottom: 10px;

					}

					.qtuser_box {
						display: flex;
						color: #9499a0;
						width: 268px;
						justify-content: space-between;
						margin-bottom: 7px;

						.qtuser_box_img {
							display: flex;
							align-items: center;

							image {
								width: 16px;
								height: 16px;
								margin-right: 6px;
							}

							.qtuser_box_hui {}
						}

					}

					.line {
						width: 72vw;
						border-bottom: 1px solid #2979ff;
						margin-top: 14px;
					}




				}

			}


		}
	}

	.bottom_comment {
		width: 100vw;
		position: fixed;
		bottom: 0;
		left: 0;
		display: flex;
		justify-content: space-around;
		align-items: center;
		height: 40px;
		z-index: 20;
		background-color: #fff;



		.bottom_ipt {
			border: 1px solid $uni-border-color;
			height: 30px;
			width: 130px;
			border-radius: 20px;
			box-sizing: border-box;
			padding-left: 10px;
		}

		.bottom_right {
			width: 60vw;
			display: flex;
			justify-content: space-between;

			.bottom_right_box {
				display: flex;
				color: $uni-text-color;
				align-items: center;

				image {
					width: 30px;
					height: 30px;
					margin-right: 5px;
				}

			}
		}
	}
</style>
